/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gps.servlet;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pe.gps.server.GPSFactory;
import pe.gps.service.Servicio;
import pe.gps.service.ServicioSO;
import pe.gps.service.ServicioTCP;
import pe.gps.service.ServicioWR;
import pe.gps.util.GpsStatus;

/**
 *
 * @author romulogalindo
 */
public class GPSStatusServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Content-Type", "application/json; charset=UTF-8");
        response.addHeader("Access-Control-Allow-Origin", "*");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            String xact = request.getParameter("xact");
            switch (xact) {
                case "1": {
                    Date date = new Date();
                    GpsStatus gps = new GpsStatus();
                    gps.setFecha_a(new SimpleDateFormat("hh:mm:ss").format(date));
                    gps.setFecha_b(new SimpleDateFormat("dd/MM/yyyy").format(date));
                    List<pe.gps.util.ServicioTCP> tcps = new ArrayList<>();
                    List<pe.gps.util.ServicioWR> wrs = new ArrayList<>();
                    List<pe.gps.util.ServicioSO> sos = new ArrayList<>();

                    for (Servicio s : GPSFactory.getServer().getServicios()) {
                        switch (s.getCo_type()) {
                            case "TCP": {
                                ServicioTCP tcp = (ServicioTCP) s;
                                pe.gps.util.ServicioTCP sc = new pe.gps.util.ServicioTCP();
                                sc.setId(tcp.getNo_server());
                                if (tcp.getCa_disgps() > 0) {
                                    sc.setImg("img/green.png");
                                } else {
                                    sc.setImg("img/orange.jpeg");
                                }
                                sc.setMegabytes("" + tcp.getMegabytes());
                                sc.setCa_disgps(tcp.getCa_disgps());
                                sc.setTm_min(tcp.getTm_minimo());
                                sc.setTm_max(tcp.getTm_maximo());
                                sc.setRecords(tcp.getRecords());
                                tcps.add(sc);
                                break;
                            }
                            case "WR": {
                                ServicioWR wr = (ServicioWR) s;
                                pe.gps.util.ServicioWR sc = new pe.gps.util.ServicioWR();
                                sc.setId(wr.getNo_server());
                                if (wr.getCa_process() > 0) {
                                    sc.setImg("img/green.png");
                                } else {
                                    sc.setImg("img/orange.jpeg");
                                }
                                sc.setSizeThread("" + wr.getSizethread());
                                sc.setRegistros("" + wr.getCa_process());
                                sc.setAtendidos("" + wr.getCa_process_ok());
                                sc.setPendientes("" + wr.getCa_process_wait());
                                sc.setPs("" + wr.getCa_ps());
                                wrs.add(sc);
                                break;
                            }
                            case "SO": {
                                ServicioSO so = (ServicioSO) s;
                                pe.gps.util.ServicioSO sc = new pe.gps.util.ServicioSO();
                                sc.setId("" + so.getNo_server());
                                sc.setExe("" + so.getCa_exec());
                                sos.add(sc);
                                break;
                            }
                        }
                    }
                    gps.setTCP(tcps);
                    gps.setWR(wrs);
                    gps.setSO(sos);
                    String json = new Gson().toJson(gps);
                    out.println(json);
                    break;
                }
                case "2": {
                    long date = new Date().getTime();
                    long ca_disgps = 0;
                    long ca_pungps = 0;
                    for (Servicio s : GPSFactory.getServer().getServicios()) {
                        if (s.getCo_type().contentEquals("TCP")) {
                            ServicioTCP tcp = (ServicioTCP) s;
                            ca_disgps = ca_disgps + tcp.getCa_disgps();
                            ca_pungps = ca_pungps + tcp.getRecords();
                        }
                    }
                    String json = "{"
                            + "\"fe_sergps\" : " + date + ","
                            + "\"ca_disgps\" : " + ca_disgps + ","
                            + "\"ca_pungps\" : " + ca_pungps
                            + "}";
                    out.println(json);
                    break;
                }
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
